﻿/// <reference path="jquery-bootgrid/dist/jquery.bootgrid.bykenny.js" />
;(function () {
    'use strict';
    var exporter = window.BT || {};
    var defaultGridOpt = {
        labels: {
            all: "全部",
            infos: "显示{{ctx.start}}至{{ctx.end}},共{{ctx.total}}条数据",
            loading: "加载中,请稍等...",
            noResults: "对不起,暂无数据！",
            refresh: "刷新",
            search: "查找"
        },
        sorting: true,//默认不排序
        ajax: true,
        responseHandler: function (response) {
            if (response && response.rows && response.rows.length > 0) {
                $.each(response.rows, function (i, item) {
                    response.rows[i].Index = (response.current - 1) * response.rowCount + i + 1;
                });
                return response;
            }
            return response;
        },
        events: []
    };
    var noLables = {
        all: "全部",
        infos: '',
        loading: "加载中,请稍等...",
        noResults: "对不起,暂无数据！",
        refresh: "刷新",
        search: "查找"
    };
    var noPagerOpt = {
        labels: noLables,
        rowCount: -1
    };
    var noTotalOpt = {
        labels: noLables
    };
    var gridNameSpace = '.rs.jquery.bootgrid';

    function BGrid(opt, pager, noTotalLable) {
        var self = this;
        if (pager === true) {
            defaultGridOpt = $.extend(defaultGridOpt, noPagerOpt);
        }
        if (noTotalLable === true) {
            defaultGridOpt = $.extend(defaultGridOpt, noTotalOpt);
        }
        this.option = $.extend({}, defaultGridOpt, opt);
        $.each(self.option.events, function (i, item) {
            $(self.option.id).on(item.name + gridNameSpace, item.event);
        });
        //先绑定事件
        this.bGrid = $(this.option.id).bootgrid(this.option);
    }

    var bgProto = BGrid.prototype;
    //刷新Grid
    bgProto.reload = function (obj) {
        obj = $.extend({}, obj);
        this.bGrid.bootgrid('setOption', 'post', obj).bootgrid('search', 'v=' + new Date().getTime());
    };
    //公共返回选中行数，没有则返回空数组
    bgProto.getSelections = function () {
        var self = this;
        var selectedRows = [];
        $('tbody :checkbox:checked', this.bGrid).each(function (i, item) {
            selectedRows.push($(this).val());
        });
        console.log("这是bootGrid自带的方法:"+ $(this.option.id).bootgrid('getSelectedRows'));
        return selectedRows;
    };

    //选中节点

    bgProto.select=function (ids) {
        $(this.option.id).bootgrid("select",ids);
    };


    bgProto.getCount = function () {
        return $(this.option.id).bootgrid('getTotalRowCount');
    };
    bgProto.on = function (eventName, callback) {
        var self = this;
        return $(self.option.id).on(eventName + gridNameSpace, callback);
    };
    //grid
    exporter.BGrid = BGrid;
    window.BT = exporter;
})();